<?php
/*
 * @Author: north.Deng
 * @Date: 2017-07-20 16:48:13
 * @Last Modified by: north.Deng
 * @Last Modified time: 2017-07-20 17:34:07
 */

 error_reporting(E_ALL);
ini_set('memory_limit', '256M');
set_time_limit(0);
include_once 'aliyuncs/aliyun-php-sdk-core/Config.php';
use Green\Request\V20170112 as Green;

date_default_timezone_set("PRC");
$url = $_GET['urls'];
$ak = parse_ini_file("aliyun.ak.ini");
//请替换成你自己的accessKeyId、accessKeySecret
$iClientProfile = DefaultProfile::getProfile("cn-hangzhou", $ak["accessKeyId"], $ak["accessKeySecret"]); // TODO
DefaultProfile::addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
$client = new DefaultAcsClient($iClientProfile);
$request = new Green\ImageAsyncScanRequest();
$request->setMethod("POST");
$request->setAcceptFormat("JSON");
$task1 = array('dataId' =>  uniqid(),
    'url' => $url,
    'time' => round(microtime(true)*1000)
);
/**
 * porn: 色情
 * terrorism: 暴恐
 * qrcode: 二维码
 * ad: 图片广告
 * ocr: 文字识别
*/
$taskId = '';
$request->setContent(json_encode(array("tasks" => array($task1),
    "scenes" => array("porn"))));
try {
    $response = $client->getAcsResponse($request);
  
    if (200 == $response->code) {
        $taskResults = $response->data;
        foreach ($taskResults as $taskResult) {
            if (200 == $taskResult->code) {
                $taskId = $taskResult->taskId;
                
                // 将taskId 保存下来，间隔一段时间来轮询结果, 参照ImageAsyncScanResultsRequest
            } else {
                print_r("task process fail:" + $response->code);
            }
        }
    } else {
        print_r("detect not success. code:" + $response->code);
    }
} catch (Exception $e) {
  //  print_r($e);
}
$data = array();
$flag = true;
while ($taskId && $flag) {
    $request = new Green\ImageAsyncScanResultsRequest();
    $request->setMethod("POST");
    $request->setAcceptFormat("JSON");
    $request->setContent(json_encode(array($taskId)));
    try {
        $response = $client->getAcsResponse($request);
        if (200 == $response->code) {
            $taskResults = $response->data;
            foreach ($taskResults as $key => $taskResult) {
                if (200 == $taskResult->code) {
                    $data[$key]['url'] = $taskResult->url;
                    $sceneResults = $taskResult->results;
                    foreach ($sceneResults as $sceneResult) {
                        $data[$key]['scene'] = $sceneResult->scene;
                        if ($sceneResult->suggestion == 'pass') {
                            $data[$key]['suggestion'] = '正常';
                        } elseif ($sceneResult->suggestion == 'review') {
                            $data[$key]['suggestion'] = '需审核';
                        } elseif ($sceneResult->suggestion == 'block') {
                            $data[$key]['suggestion'] = '违规';
                        }
                        $data[$key]['rate'] = $sceneResult->rate;//结果为该分类的概率；值越高，越趋于该分类
                        if ($sceneResult->label == 'normal') {
                            $data[$key]['label'] = '正常图片，无色情';
                        } elseif ($sceneResult->label == 'sexy') {
                            $data[$key]['label'] = '性感图片';
                        } elseif ($sceneResult->label == 'porn') {
                            $data[$key]['label'] = '色情图片';
                        }
                        echo json_encode($data);exit;
                    }
                } else {
                   // print_r("task process fail:" + $response->code);
                }
            }
            
        } else {
           // print_r("detect not success. code:" + $response->code);
        }
    } catch (Exception $e) {
       // print_r($e);
    }
    sleep(1);
}
